﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ThreadDemo
{
    class Program
    {
        private static object objLock = new object();


        static Stopwatch sp = new Stopwatch();
        public static double currentTime
        {
            get => sp.Elapsed.TotalSeconds;
        }


        static void Main(string[] args)
        {
            //sp.Restart();
            //Thread.Sleep(2000);
            //Console.WriteLine("sp:" + sp.ElapsedMilliseconds);

            Test2();
        }

        private static void Test()
        {
            Task task1 = new Task(() => { Console.WriteLine("线程1"); });
            task1.Start();
            Task task2 = Task.Run(() => { Console.WriteLine("线程1"); });
            Task task3 = Task.Factory.StartNew(() => { Console.WriteLine("线程1"); });
            Console.ReadKey();
        }


        private static void Test2()
        {
            Thread ActionThread = new Thread(new ThreadStart(AddText))
            {
                IsBackground = true
            };
            ActionThread.Start();

            //var thread = new Thread(AddText);
            //thread.Start();
            Console.WriteLine("文件下载完成");
            Console.WriteLine("退出主程序");
            Console.ReadKey();
        }


        private static void ThreadTest()
        {
            for (int i = 0; i < 10; i++)
            {
                //var thread = new Thread(AddText);
                //thread.Start();

                Thread ActionThread = new Thread(new ThreadStart(AddText))
                {
                    IsBackground = true
                };
                ActionThread.Start(i);
                //Thread.Sleep(1000);
            }
            Console.WriteLine("文件下载完成");
            Console.WriteLine("退出主程序");
            Console.ReadKey();
        }

        static void AddText()
        {
            int i = 0;

            while (true)
            {
                i++;
                lock (objLock)
                {
                    sp.Restart();

                    Thread.Sleep(2000);
                    Console.WriteLine("sp1:" + sp.ElapsedMilliseconds);
                    Console.WriteLine("currentTime1:" + currentTime);
                    File.AppendAllText(@"ThreadTest.txt", $"开始：{Thread.CurrentThread.ManagedThreadId},i:{i}");
                    Console.WriteLine("开始:" + i);

                    for (int a = 0; a < 999999999; a++)
                    {
                        // Console.WriteLine(a);
                    }

                    Thread.Sleep(2000);
                    Console.WriteLine("sp2:" + sp.ElapsedMilliseconds);
                    Console.WriteLine("currentTime2:" + currentTime);
                    // File.AppendAllText(@"ThreadTest.txt", $"结束：{Thread.CurrentThread.ManagedThreadId}");
                    Console.WriteLine("结束:" + i);
                }

                Console.WriteLine("出来了" + i);

                Console.WriteLine("............");

                Thread.Sleep(1000);
            }

        }

    }
}
